package com.payment.ktb.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.payment.ktb.application.MyApplication;


/**
 * Created by Android Studio
 * User: fei.wang
 * Date: 2016-03-15
 * Time: 9:57
 * Description: 保存简单基本数据类型的工具类
 */
public class SharedPreferencesUtils {
    private static SharedPreferences mSharedPreferences = null;
    private static SharedPreferences.Editor mEditor = null;


    public static SharedPreferences.Editor getInstance() {
        if (mSharedPreferences == null) {
            mSharedPreferences = MyApplication.getContext().getSharedPreferences("jnsh_sharepreference",Context.MODE_PRIVATE);
        }
        mEditor = mSharedPreferences.edit();
        return mEditor;
    }

    /**
     * 初始化SharePreference
     *
     */
    public static void init() {
        mSharedPreferences = MyApplication.getContext().getSharedPreferences("jnsh_sharepreference",Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }


    public static String getString(String key, String defValue) {
        return mSharedPreferences.getString(key, defValue);
    }

    public static String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return mSharedPreferences.getBoolean(key, defValue);
    }

    public static int getInteger(String key) {
        return mSharedPreferences.getInt(key, -1);
    }

    public static int getInteger(String key, int defValue) {
        return mSharedPreferences.getInt(key, defValue);
    }

    public static long getLong(String key) {
        return mSharedPreferences.getLong(key, -1);
    }

    public static long getLong(String key, int value) {
        return mSharedPreferences.getLong(key, -1);
    }

    public static float getFloat(String key) {
        return mSharedPreferences.getFloat(key, -1);
    }

    public static boolean putString(String key, String value) {
        mEditor.putString(key, value);
        return mEditor.commit();
    }

    public static boolean putBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        return mEditor.commit();
    }

    public static boolean putFloat(String key, float value) {
        mEditor.putFloat(key, value);
        return mEditor.commit();
    }

    public static boolean putLong(String key, long value) {
        mEditor.putLong(key, value);
        return mEditor.commit();
    }

    public static boolean putInteger(String key, int value) {
        mEditor.putInt(key, value);
        return mEditor.commit();
    }

    public static boolean removeKey(String key) {
        mEditor.remove(key);
        return mEditor.commit();
    }

    public static boolean clear() {
        mEditor.clear();
        return mEditor.commit();
    }

}
